package com.ywjg.serviceimp;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ywjg.dao.AppGoodDao;
import com.ywjg.dao.AppShopDao;
import com.ywjg.dao.GoodCartDao;
import com.ywjg.dao.GoodCommentDao;
import com.ywjg.dao.GoodToOrderDao;
import com.ywjg.model.AppShop;
import com.ywjg.model.GoodCart;
import com.ywjg.model.GoodCartsAndOrder;
import com.ywjg.model.GoodComment;
import com.ywjg.model.GoodOrder;
import com.ywjg.model.GoodToOrder;
import com.ywjg.service.IGoodToOrderService;
import com.ywjg.utils.ApplicationModel;

@Service
public class GoodToOrderService implements IGoodToOrderService  {
	@Resource
	GoodToOrderDao goodToOrderDao;
	@Resource
	GoodCartDao goodCartDao;
	@Resource
	AppGoodDao appGoodDao;
	@Resource
	GoodCommentDao goodCommentDao;
	@Resource
	AppShopDao  appShopDao;
	@Autowired
	ApplicationModel applicationModel;
	
    /* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IGoodToOrderService#addItemGoodToOrder(com.ywjg.model.GoodOrder, com.ywjg.model.GoodCartsAndOrder)
	 */
	@Transactional
	public void addItemGoodToOrder(GoodOrder GoodOrder, GoodCartsAndOrder goodCartsAndOrder) {
		List<Integer> goodCartsId = goodCartsAndOrder.getGoodCarts();
		List<GoodCart> goodCarts = new ArrayList<GoodCart>();
		for(Integer goodCartId:goodCartsId) {
			GoodCart oneGoodCart = goodCartDao.findOne(goodCartId);
			goodCarts.add(oneGoodCart);
		}
		
		for (GoodCart goodCart : goodCarts) {
			
			GoodToOrder goodToOrder = new GoodToOrder();

			goodToOrder.setGoodId(goodCart.getGoodId());
			
			goodToOrder.setReciveAddress(GoodOrder.getOrderBuyerInf());
			
			goodToOrder.setShopId(goodCart.getShopId());
			
			goodToOrder.setUserId(goodCart.getUserId());
			
			goodToOrder.setOrderId(GoodOrder.getId());

			goodToOrder.setGoodModelId(goodCart.getGoodModelId());
			
			goodToOrder.setGoodNumber(goodCart.getGoodNumber());
			
			goodToOrder.setGoodName(goodCart.getGoodName());
			
			goodToOrder.setGoodPrice(goodCart.getGoodPrice());
			
			goodToOrder.setGoodShopName(goodCart.getGoodShopName());
			
			goodToOrder.setGoodLogo(goodCart.getGoodLogo());

			goodToOrderDao.save(goodToOrder);

		}

	}
	
	
	/* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IGoodToOrderService#getGoodFromOrder(int)
	 */
	public List<GoodToOrder> getGoodFromOrder(int orderId) {
		List<GoodToOrder> gtrs = goodToOrderDao.findByOrderId(orderId);
		return gtrs;

	}
	
	/* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IGoodToOrderService#getNotSendGood(int)
	 */
	public List<GoodToOrder> getNotSendGood(int userId){
		
		int shopId = appShopDao.findByAppUserId(userId).getId();
		
		List<GoodToOrder> goodToOrders = goodToOrderDao.findByIsSendAndShopId(0,shopId);
		
		return goodToOrders;
		
	}
	
      /* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IGoodToOrderService#refreshIsSend(int, java.lang.String)
	 */
    public GoodToOrder refreshIsSend(int id,String trackingNumber){
    	  GoodToOrder gto = goodToOrderDao.findOne(id);
    	  gto.setIsSend(1);
    	  gto.setTrackingNumber(trackingNumber);
    	  return goodToOrderDao.save(gto);
	}
      
      /* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IGoodToOrderService#refreshIsRecive(int)
	 */
    public GoodToOrder refreshIsRecive(int id){
    	  GoodToOrder gto = goodToOrderDao.findOne(id);
    	  gto.setIsRecive(1);
    	  return goodToOrderDao.save(gto);
	}
      
      
      /* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IGoodToOrderService#refreshIsComment(int)
	 */
    public GoodToOrder refreshIsComment(int id){
    	  GoodToOrder gto = goodToOrderDao.findOne(id);
    	  gto.setIsComment(1);
    	  return goodToOrderDao.save(gto);
	 }
      
      /* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IGoodToOrderService#getNotCommentGood(int)
	 */
    public List<GoodToOrder> getNotCommentGood(int userId){
    	  List<GoodToOrder> goodToOrders = goodToOrderDao.findByIsReciveAndUserIdAndIsComment(1, userId, 0);
    	  return goodToOrders;
	 }
      
      
      /* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IGoodToOrderService#getShouldReciveGood(int)
	 */
    public List<GoodToOrder> getShouldReciveGood(int userId){
    	  List<GoodToOrder> goodToOrders = goodToOrderDao.findByIsReciveAndUserId(0, userId);
    	  return goodToOrders;
	 }
      
      
      /* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IGoodToOrderService#commentGood(com.ywjg.model.GoodComment)
	 */
    public GoodComment commentGood(GoodComment goodComment){
    	  GoodComment gc = goodCommentDao.save(goodComment);
    	  return gc;
	 }
      
      
      /* (non-Javadoc)
	 * @see com.ywjg.serviceimp.IGoodToOrderService#getAllSaleGood(int, int, int)
	 */
    public List<GoodToOrder> getAllSaleGood(int userId,int page,int size){
    	  Pageable pageable = new PageRequest(page, size);
    	  AppShop shop = appShopDao.findByAppUserId(userId);
    	  if (null==shop) {
			return null;
		}
    	  int shopId = shop.getId();
    	  Page<GoodToOrder> gtos = goodToOrderDao.findByShopIdAndIsSend(shopId,1,pageable);
    	  int numberOfElements = gtos.getNumberOfElements();
    	  applicationModel.getMap().put("getAllSaleGood", (int)numberOfElements);
    	  return gtos.getContent();
	 }
     
    @Transactional
    public void submitCommentGood(int id){
  	   GoodToOrder findOne = goodToOrderDao.findOne(id);
  	   findOne.setIsComment(1);
  	    goodToOrderDao.save(findOne);
	 }

}
